﻿using System;
using com.yoozoo.gta.Extension.UGUI.ScreenOrientationEx;
using com.yoozoo.gta.Tools.UI;
using UnityEngine;

namespace com.yoozoo.gta.Extension.UGUI.ScreenOrientation.Adapter
{

    [Serializable]
    public class MarginScalerConfig
    {
        public bool left = true;
        public bool right = true;
        public bool top = false;
        public bool bottom = false;
        public int marginMinDelta;
        public int marginMaxDelta;
        /// <summary>
        /// 新增第2套 数值 用于right 和bottom
        /// </summary>
        public int secondMarginMinDelta;
        public int secondMarginMaxDelta;
        
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return false;
            }

            MarginScalerConfig other = obj as MarginScalerConfig;
            if (left != other.left)
            {
                return false;
            }
            
            if (right != other.right)
            {
                return false;
            }
            
            if (top != other.top)
            {
                return false;
            }
            
            if (bottom != other.bottom)
            {
                return false;
            }
            
            if (marginMinDelta != other.marginMinDelta)
            {
                return false;
            }
            
            if (marginMaxDelta != other.marginMaxDelta)
            {
                return false;
            }
            
            if (secondMarginMinDelta != other.secondMarginMinDelta)
            {
                return false;
            }
            
            if (secondMarginMaxDelta != other.secondMarginMaxDelta)
            {
                return false;
            }

            return true;
        }
        
        public override int GetHashCode()
        {
            int hash = 17;
            hash = hash * 23 + left.GetHashCode();
            hash = hash * 23 + right.GetHashCode();
            hash = hash * 23 + top.GetHashCode();
            hash = hash * 23 + bottom.GetHashCode();
            hash = hash * 23 + marginMinDelta.GetHashCode();
            hash = hash * 23 + marginMaxDelta.GetHashCode();
            hash = hash * 23 + secondMarginMinDelta.GetHashCode();
            hash = hash * 23 + secondMarginMaxDelta.GetHashCode();
            return hash;
        }
    }

    [DisallowMultipleComponent]
    public class MarginScalerAdapter: AdaptBase<MarginScalerConfig, MarginScaler>
    {
        public override int Order => (int)AdapterExecuteOrder.Default; 
        protected override void CopyProperty(MarginScalerConfig config)
        {
            base.CopyProperty(config);
            config.left = mComponent.left;
            config.right = mComponent.right;
            config.top = mComponent.top;
            config.bottom = mComponent.bottom;
            config.marginMinDelta = mComponent.marginMinDelta;
            config.marginMaxDelta = mComponent.marginMaxDelta;
            config.secondMarginMinDelta = mComponent.secondMarginMinDelta;
            config.secondMarginMaxDelta = mComponent.secondMarginMaxDelta;
        }

        //生成GetHashCode方法
        protected override void ApplyConfig(MarginScalerConfig config)
        {
            base.ApplyConfig(config);
            mComponent.left = config.left;
            mComponent.right = config.right;
            mComponent.top = config.top;
            mComponent.bottom = config.bottom;
            mComponent.marginMinDelta = config.marginMinDelta;
            mComponent.marginMaxDelta = config.marginMaxDelta;
            //老的prefab right 和bottom 使用原来的数值
            if (config.secondMarginMinDelta == 0 && config.secondMarginMaxDelta == 0)
            {
                mComponent.secondMarginMinDelta = config.marginMinDelta;
                mComponent.secondMarginMaxDelta = config.marginMaxDelta;
            }
            else
            {
                mComponent.secondMarginMinDelta = config.secondMarginMinDelta;
                mComponent.secondMarginMaxDelta = config.secondMarginMaxDelta;
            }

            mComponent.RefreshMargin();
        }
    }

   
}